package com.tangkun.environment.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tangkun.environment.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通用对话框
 *
 * @author TangKun
 */
public class CommonQueDingDialog extends Dialog {
    @BindView(R.id.tv_common_title)
    TextView tvTitle;
    @BindView(R.id.tv_common_content)
    TextView tvContent;
    @BindView(R.id.btn_common_right)
    Button btnRight;
    private Context context;
    private View view;

    private String title = "";
    private String content = "";
    private String rightBtnContent = "";


    public CommonQueDingDialog(Context context) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        view = initView();
    }

    /**
     * @param context 上下文
     * @param title   标题
     * @param content 内容
     */
    public CommonQueDingDialog(Context context, String title, String content) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        this.title = title;
        this.content = content;
        view = initView();
    }

    /**
     * @param context         上下文
     * @param content         对话框中的内容
     * @param rightBtnContent 确定按钮文字
     */
    public CommonQueDingDialog(Context context, String title, String content, String rightBtnContent) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        this.title = title;
        this.content = content;
        this.rightBtnContent = rightBtnContent;
        view = initView();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        requestWindowFeature(Window.PROGRESS_INDETERMINATE_ON);
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getWindow().setWindowAnimations(R.style.InvitationDialogStyle);
        setCancelable(false);//不可以被返回键删除
        view = initView();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dm.widthPixels, dm.heightPixels);
        addContentView(view, lp);
        setCanceledOnTouchOutside(true);
    }


    private View initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_common_queding, null);
        ButterKnife.bind(this, view);
        //有自定义标题
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        //有自定义内容
        if (!TextUtils.isEmpty(content)) {
            tvContent.setText(content);
        }
        //右边边按钮文字
        if (!TextUtils.isEmpty(rightBtnContent)) {
            btnRight.setText(rightBtnContent);
        }
        return view;
    }


    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private OnClickListener onClickListener;

    public interface OnClickListener {
        void rightClick();
    }

    @OnClick({R.id.btn_common_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_common_right://右边按钮监听
                dismiss();
                if (onClickListener != null) {
                    onClickListener.rightClick();
                }
                break;
        }
    }
}
